home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / Networking / GetPPPStatus / sources / GetPPPStatus.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-02-06  |  6.5 KB  |  293 lines  |  [TEXT/CWIE]

  1. /*
  2.  */
  3.  
  4. #include "AEHelpers.h"
  5. #include <ASRegistry.h>
  6. #include <AppleEvents.h>
  7. #include <Fonts.h>
  8. #include <Events.h>
  9. #include <Files.h>
  10. #include <StandardFile.h>
  11. #include <Types.h>
  12. #include <Gestalt.h>
  13.  
  14. #include <stdio.h>
  15. #include <String.h>
  16. #include <stdlib.h>
  17. #include <Events.h>
  18. #include "OpenTptPPP.h"
  19. #include "MoreFinderEvents.h"
  20. #include "RAScriptingAdditions.h"
  21.  
  22. #define kFinderType         'FNDR'
  23. #define kFinderCreator        'MACS'
  24.  
  25. //************** Global definitions *********************//
  26. AEEventID    gPPPEventID;
  27.     
  28. //****************** Prototypes *************************//
  29. Boolean    NetInit(void);
  30. pascal Boolean MyIdleFunction(EventRecord *event, long *sleep, RgnHandle mousergn);
  31. void Initialize(void);
  32. void ShowRAStatusResults(AppleEvent *aevent);
  33.  
  34. Boolean NetInit()
  35. {
  36.     OSStatus err;
  37.     UInt32 gResult;
  38.  
  39.         // determine whether OT/PPP or ARA 3.0 is present
  40.     err = Gestalt(gestaltOpenTptRemoteAccess, (long*) &gResult);
  41.     
  42.     if (err || (gResult == 0))
  43.     {
  44.         fprintf(stderr, "\nThis sample requires OT/PPP 1.0 or ARA 3.0");
  45.         return false;
  46.     }
  47.     
  48.         // if gestaltOpenTptRemoteAccess is present, determine whether OT/PPP or ARA 3.0 is present
  49.  
  50.     err = Gestalt(gestaltOpenTptRemoteAccessVersion, (long*) &gResult);
  51.     if (err)
  52.     {
  53.         fprintf(stderr, "\nThere was a problem determining the OT/PPP version present %ld", err);
  54.         return false;
  55.     }
  56.     
  57.     if (gResult >= kARA30Version)
  58.         gPPPEventID = kARAStatusRequestID;    // ARA 3.0 is present
  59.     else if (gResult != 0)
  60.         gPPPEventID = kPPPStatusRequestID;
  61.     else
  62.     {
  63.         fprintf(stderr, "\nThere was a problem determining the OT/PPP version present");
  64.         return false;
  65.     }
  66.     
  67.     return true;
  68. }
  69.  
  70.  
  71. void Initialize(void)
  72. {
  73.     /* Initialize all the needed managers. */
  74.     InitGraf(&qd.thePort);
  75.     InitFonts();
  76.     InitWindows();
  77.     InitMenus();
  78.     TEInit();
  79.     InitDialogs(nil);
  80.     InitCursor();
  81. }
  82.  
  83. void main(void)
  84. {
  85.     AppleEvent            reqEvent = { typeNull, nil };
  86.     AppleEvent            retEvent = { typeNull, nil };
  87.     AEDesc                firstRec = { typeNull, nil };
  88.     AEDesc                aeRec = { typeNull, nil };
  89.     AEDesc                baudRateDesc = { typeNull, nil };
  90.     AEDescList            aelist;
  91.     AERecord            aerecord;
  92.     AEIdleUPP            idleUPP;
  93.     OSErr                err;
  94.     
  95.     Initialize();
  96.  
  97.     if (NetInit() == false)
  98.         return;
  99.     
  100.         // set up the Finder as the target of the OT/PPP or ARA Apple Event call
  101.     err = AEHMakeAppleEventSignatureTarget(kFinderType, kFinderCreator, kNetworkAccessClass,
  102.                                             gPPPEventID, &reqEvent);
  103.     
  104.     if (err != noErr)
  105.     {
  106.         printf("Error calling AEHMakeAppleEventSignatureTarget: %d", err);
  107.         return;
  108.     }        
  109.  
  110.         // send the Apple event to the Finder for processing.
  111.     idleUPP = NewAEIdleProc(MyIdleFunction);
  112.     err = AESend(&reqEvent, &retEvent, kAEWaitReply+kAENeverInteract, kAENormalPriority,
  113.                     kAEDefaultTimeout, idleUPP, nil);
  114.                     
  115.     if (err != noErr)
  116.     {
  117.         printf("Error calling AESend = %d", err);
  118.     }
  119.     else
  120.     {
  121.             // get the apple event object
  122.         err = AEGetParamDesc(&retEvent, keyDirectObject, typeAEList, &aelist);
  123.         if (err == noErr)
  124.         {
  125.                 // get the apple event record 
  126.             err = AEGetParamDesc(&aelist, typeAERecord, typeAERecord, &aerecord);
  127.             
  128.             if (err == noErr)
  129.             {
  130.                     // call the function that will parse the return info
  131.                 ShowRAStatusResults(&aerecord);
  132.             }
  133.             else
  134.             {
  135.                 printf("Error calling AEGetParamDesc to get aerecord %d", err);
  136.             }
  137.             
  138.             AEDisposeDesc(&aelist);
  139.             
  140.         }
  141.         else
  142.             printf("Error calling AEGetParamDesc to getaelist %d", err);
  143.  
  144.         
  145.         AEDisposeDesc(&retEvent);
  146.     }
  147.     
  148.     AEDisposeDesc(&reqEvent);
  149. }
  150.  
  151. pascal Boolean MyIdleFunction(EventRecord *event, long *sleep, RgnHandle mousergn)
  152. {
  153.     Boolean    result = false;
  154.     
  155. //    DebugStr("\pIn idle function");
  156.     
  157.     return result;
  158. }        
  159.  
  160. void ShowRAStatusResults(AppleEvent *aerecord)
  161. {
  162.     AEDesc                desc = { typeNull, nil };
  163.     UInt32                *l;
  164.     Size                size;
  165.     OSErr                err;
  166.     char                str[256];
  167.     
  168.     err = AEGetParamDesc(aerecord, kRAStateKey, typeChar, &desc);
  169.     if (err == noErr)
  170.     {
  171.         size = GetHandleSize(desc.dataHandle);
  172.         if (size != 0)
  173.         {
  174.                 // get the connect state into a local string
  175.             BlockMove(*(desc.dataHandle), str, size);
  176.             str[size] = 0;
  177.             printf("\nConnection State - %s", str);
  178.         }
  179.         AEDisposeDesc(&desc);
  180.     }
  181.     else
  182.     {
  183.         printf("\nError calling AEGetParamDesc to get connection: %d", err);
  184.         return;
  185.     }
  186.  
  187.     err = AEGetParamDesc(aerecord, kRASecsConnectKey, typeInteger, &desc);
  188.     if (err == noErr)
  189.     {
  190.         l = (UInt32*)*desc.dataHandle;
  191.         printf("\nSeconds Connected - %ld", *l);
  192.         AEDisposeDesc(&desc);
  193.     }
  194.     else
  195.     {
  196.         printf("\nError calling AEGetParamDesc to get kRASecsConnectKey: %d", err);
  197.     }
  198.     
  199.     err = AEGetParamDesc(aerecord, kRASecsRemainKey, typeInteger, &desc);
  200.     if (err == noErr)
  201.     {
  202.         l = (UInt32*)*desc.dataHandle;
  203.         printf("\nSeconds Reamining - %ld", *l);
  204.         AEDisposeDesc(&desc);
  205.     }
  206.     else
  207.     {
  208.         printf("\nError calling AEGetParamDesc to get kRASecsConnectKey: %d", err);
  209.     }
  210.  
  211.     err = AEGetParamDesc(aerecord, kRAMessageKey, typeChar, &desc);
  212.     if (err == noErr)
  213.     {
  214.         size = GetHandleSize(desc.dataHandle);
  215.         if (size != 0)
  216.         {
  217.                 // get the message text into a local string
  218.             BlockMove(*(desc.dataHandle), str, size);
  219.             str[size] = 0;
  220.             printf("\nConnection Message - %s", str);
  221.         }
  222.         AEDisposeDesc(&desc);
  223.     }
  224.     else
  225.     {
  226.         printf("\nError calling AEGetParamDesc to get the message: %d", err);
  227.     }
  228.  
  229.     if (gPPPEventID >= kARAStatusRequestID)  // to check for protocol, check if ARA 3.0 is present
  230.     {
  231.         err = AEGetParamDesc(aerecord, kRAProtocolKey, typeChar, &desc);
  232.         if (err == noErr)
  233.         {
  234.             size = GetHandleSize(desc.dataHandle);
  235.             if (size != 0)
  236.             {
  237.                     // get the protocol type text into a local string
  238.                 BlockMove(*(desc.dataHandle), str, size);
  239.                 str[size] = 0;
  240.                 printf("\nProtocol - %s", str);
  241.             }
  242.             AEDisposeDesc(&desc);
  243.         }
  244.         else
  245.         {
  246.             printf("\nError calling AEGetParamDesc to get protocol: %d", err);
  247.         }
  248.     }
  249.     
  250.     err = AEGetParamDesc(aerecord, kBaudRateKey, typeChar, &desc);
  251.     if (err == noErr)
  252.     {
  253.         size = GetHandleSize(desc.dataHandle);
  254.         if (size != 0)
  255.         {
  256.                 // get the baud rate text into a local string
  257.             BlockMove(*(desc.dataHandle), str, size);
  258.             str[size] = 0;
  259.             printf("\Baud Rate - %s", str);
  260.         }
  261.         AEDisposeDesc(&desc);
  262.     }
  263.     else
  264.     {
  265.         printf("\nError calling AEGetParamDesc to get baud rate: %d", err);
  266.     }
  267.     
  268.     err = AEGetParamDesc(aerecord, kRABytesInKey, typeInteger, &desc);
  269.     if (err == noErr)
  270.     {
  271.         l = (UInt32*)*desc.dataHandle;
  272.         printf("\nNum Bytes In - %ld", *l);
  273.         AEDisposeDesc(&desc);
  274.     }
  275.     else
  276.     {
  277.         printf("\nError calling AEGetParamDesc to get the number of bytes in: %d", err);
  278.     }
  279.     
  280.     err = AEGetParamDesc(aerecord, kRABytesOutKey, typeInteger, &desc);
  281.     if (err == noErr)
  282.     {
  283.         l = (UInt32*)*desc.dataHandle;
  284.         printf("\nBytes Out - %ld", *l);
  285.         AEDisposeDesc(&desc);
  286.     }
  287.     else
  288.     {
  289.         printf("\nError calling AEGetParamDesc to get the number of bytes out: %d", err);
  290.     }
  291.     
  292. }    
  293.